//
//  SDGetMoreView.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDGetMoreView.h"

@implementation SDGetMoreView

+(NSUInteger)getHeight
{
    return LAYOUT_HEIGHT;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        // Initialization code
        gradientBackground_ = YES;
        _customTextLabel = nil;
        
        // UI Element
        _customTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _customTextLabel.font = [UIFont systemFontOfSize:15.0];
        _customTextLabel.backgroundColor = [UIColor clearColor];
        _customTextLabel.textAlignment = NSTextAlignmentCenter;
        _customTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self addSubview:_customTextLabel];
        
        waitingView_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        waitingView_.alpha = 1.0f;
        waitingView_.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        waitingView_.backgroundColor = [UIColor clearColor];
        
        
        //self.loadingView = [[UIImageView alloc] initWithImage:LOAD_IMAGE_USE_CACHE_INBUNDLE(TimelineBundle, @"icon_hot_timeline_pull_history.png")];
        [self.loadingView sizeToFit];
        [self addSubview:self.loadingView];
        self.loadingView.centerX = self.centerX;
        self.loadingView.centerY = self.height * 0.5;
        
        self.backgroundColor = [UIColor clearColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
      //  _customTextLabel.textColor = [UIColor colorWithRGB:0xa2a5a8];
        
        self.style = QNGetMoreTableViewCellStyleGetMore;
    }
    return self;
}

- (void) startRotateAnimation
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = 2.0;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 20;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self.loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void) stopRotateAnimation
{
    [self.loadingView.layer removeAllAnimations];
}
-(void)setStyle:(QNGetMoreTableViewCellStyle)style
{
    if( _style == style )return;
    
    _style = style;
    
    switch (style)
    {
        case QNGetMoreTableViewCellStyleGetMore:
        {
            //			[waitingView_ stopAnimating];
            //			[waitingView_ removeFromSuperview];
            self.customTextLabel.text = self.moreTipString?self.moreTipString:@"上拉加载更多";
            [self stopRotateAnimation];
            self.loadingView.hidden = YES;
            self.customTextLabel.hidden = NO;
        }
            break;
            
        case QNGetMoreTableViewCellStyleAllLoad:
        {
            //			[waitingView_ stopAnimating];
            //			[waitingView_ removeFromSuperview];
            self.customTextLabel.text = self.nomoreTipString?self.nomoreTipString:@"没有更多了";
            self.customTextLabel.hidden = NO;
            [self stopRotateAnimation];
            self.loadingView.hidden = YES;
            
        }
            break;
            
        case QNGetMoreTableViewCellStyleWaiting:
        {
            //			self.customTextLabel.text = @"正在加载...";
            //			[waitingView_ startAnimating];
            self.loadingView.hidden = NO;
            [self startRotateAnimation];
            self.customTextLabel.hidden = YES;
        }
            break;
            
        case QNGetMoreTableViewCellStyleFresh:
        {
            //            self.customTextLabel.text = self.refreshTipString?self.refreshTipString:@"释放加载更多";
        }
            break;
        default:
            break;
    }
    
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    switch (_style)
    {
        case QNGetMoreTableViewCellStyleGetMore:
        case QNGetMoreTableViewCellStyleAllLoad:
        {
            CGFloat labelW = width;
            CGFloat labelH = height;
            CGRect	labelFrame = CGRectMake(0, -1, labelW, labelH - 1);
            _customTextLabel.frame = labelFrame;
            _customTextLabel.textAlignment = NSTextAlignmentCenter;
        }
            break;
            
        case QNGetMoreTableViewCellStyleWaiting:
        {
            CGFloat labelX = 0;
            CGFloat labelY = -1;
            CGFloat labelW = width;
            CGFloat labelH = height;
            CGRect	labelFrame = CGRectMake(labelX, labelY, labelW, labelH);
            _customTextLabel.textAlignment = NSTextAlignmentCenter;
            _customTextLabel.frame = labelFrame;
            
            CGSize textSize = [_customTextLabel.text sizeWithAttributes:@{NSFontAttributeName: _customTextLabel.font}];
            
            CGFloat waitW = 18.0f;
            CGFloat waitH = 18.0f;
            CGFloat waitX = (int)((width - textSize.width) / 2.0f - 20.0f);
            CGFloat waitY = (int)((height - waitH) / 2.0f);
            waitingView_.frame = CGRectMake(waitX, waitY, waitW, waitH);
        }
            break;
            
        default:
            break;
    }
    
    //	[self setNeedsLayout];
}

-(void)layoutSubviews
{
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    [super layoutSubviews];
    
    // do nothing.
    switch (_style)
    {
        case QNGetMoreTableViewCellStyleGetMore:
        case QNGetMoreTableViewCellStyleAllLoad:
        {
            CGFloat labelW = width;
            CGFloat labelH = height;
            CGRect	labelFrame = CGRectMake(0, -1, labelW, labelH - 1);
            _customTextLabel.frame = labelFrame;
            _customTextLabel.textAlignment = NSTextAlignmentCenter;
        }
            break;
            
        case QNGetMoreTableViewCellStyleWaiting:
        {
            CGFloat labelX = 0;
            CGFloat labelY = -1;
            CGFloat labelW = width;
            CGFloat labelH = height;
            CGRect	labelFrame = CGRectMake(labelX, labelY, labelW, labelH);
            _customTextLabel.textAlignment = NSTextAlignmentCenter;
            _customTextLabel.frame = labelFrame;
            
            CGSize textSize = [_customTextLabel.text sizeWithAttributes:@{NSFontAttributeName: _customTextLabel.font}];
            
            CGFloat waitW = 18.0f;
            CGFloat waitH = 18.0f;
            CGFloat waitX = (int)((width - textSize.width) / 2.0f - 20.0f);
            CGFloat waitY = (int)((height - waitH) / 2.0f);
            waitingView_.frame = CGRectMake(waitX, waitY, waitW, waitH);
        }
            break;
            
        default:
            break;
    }
    
    [self setNeedsDisplay];
}


- (BOOL)canLoadMore
{
    return _style == QNGetMoreTableViewCellStyleGetMore || _style == QNGetMoreTableViewCellStyleFresh;
}

- (void)setHasMoreData:(BOOL)hasMoreData
{
    _hasMoreData = hasMoreData;
    
    if( _style == QNGetMoreTableViewCellStyleGetMore && !hasMoreData )
        self.style = QNGetMoreTableViewCellStyleAllLoad;
    
    else if( _style == QNGetMoreTableViewCellStyleAllLoad && hasMoreData )
        self.style = QNGetMoreTableViewCellStyleGetMore;
}

- (void)readyToStartLoading
{
    if( [self canLoadMore ] )self.style = QNGetMoreTableViewCellStyleFresh;
}

- (void)startLoading
{
    if( [self canLoadMore ] )self.style = QNGetMoreTableViewCellStyleWaiting;
}

- (void)finishLoading
{
    //    if( self.style == QNGetMoreTableViewCellStyleAllLoad || self.style == QNGetMoreTableViewCellStyleGetMore )return;
    
    if( !_hasMoreData )
    {
        if( self.style != QNGetMoreTableViewCellStyleAllLoad ) self.style = QNGetMoreTableViewCellStyleAllLoad;
    }
    else
    {
        if( self.style != QNGetMoreTableViewCellStyleGetMore ) self.style = QNGetMoreTableViewCellStyleGetMore;
    }
}


@end
